import java.util.Scanner;

import server.IListener;
import server.Listener;
import controller.IMenuController;
import controller.MenuController;
import entity.Data;
import entity.IData;
import boundary.IMenu;
import boundary.Menu;


public class Main {

	public static void main(String[] args) {
		int port;
		
		if (args.length == 1){
			try {
			port = Integer.parseInt(args[0]);
			if (port > 65535 || port < 1024)
				port = 8000;	
			} catch (NumberFormatException e){
				port = 8000;
			}
		}
		else 
			port = 8000;
		
		Scanner menuScan = new Scanner(System.in);
		
		IData data = new Data();
		IMenu menu = new Menu(menuScan,port);				
		
		IMenuController menuC = new MenuController(menu, data);
		IListener listen = new Listener(port, data, menuC);
		
		Thread menuThread = new Thread((Runnable) menuC);
		Thread listenThread = new Thread((Runnable) listen);
		
		menuThread.start();
		listenThread.start();
		
		try {
			menuThread.join();
			System.exit(0);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		menuScan.close();		
	}

}
